<template>
	<view></view>
</template>

<script>
export default {
	name: 'chooseImage',
	props:{
		payload:{
			type:Object,
			default: ()=>{}
		},
		imageType:{
			type:String,
			default:''
		}
		
	},
	data() {
		return {
			full_path_img:"",
		};
	},
	
	computed:{
		get_form_data(){
			let obj = {type:this.imageType,'x:img_type':this.imageType,...this.vuex_upload_info.data}
			return obj
		}
	},
	mounted() {
		if(!this.vuex_upload_info.url){
			uni.$u.http.get("/upload/info",{
				
			}).then(({data})=>{
				this.$u.vuex('vuex_upload_info', data||{})
				console.log('-----mounted',this.vuex_upload_info)
			}).catch(v=>{
				this.$u.toast("获取上传资源异常")
			})
		}
	},
	

	methods: {
		chooseImage(count) {
			return new Promise((resolve,reject)=>{
				uni.chooseImage({
					count: count||1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album','camera'], //从相册选择
					success: (res) => {
						const promiseData = [];
						for(let i=0;i<res.tempFilePaths.length;i++){
							promiseData.push(this.uploadFile(res.tempFilePaths[i]))
						}
						Promise.all(promiseData).then((values) => {
							resolve(values)
						}).catch(e=>{
							reject(e)
						})
					},
					fail(e) {
						reject(e)
						this.$u.toast('上传失败')
					}
				});
			})
			
		},
		uploadFile(file,params={}){
			uni.showLoading({ title: '上传中...' });
			return new Promise((resolve,reject)=>{
				uni.uploadFile({
					url: this.vuex_upload_info.url, //仅为示例，非真实的接口地址
					filePath: file,
					name: 'file',
					formData: Object.assign({},this.get_form_data,params),
					success: (uploadFileRes) => {
						var res = uploadFileRes.data||{}
						if(typeof uploadFileRes.data==='string'){
							res =JSON.parse(uploadFileRes.data)
						}
						resolve(res.data||{})
						uni.hideLoading()
					},
					fail(e) {
						reject(e)
						this.$u.toast('上传失败')
					}
				});
			});	
		}
	}
};
</script>

<style lang="scss">
	.uploader-body {
		width: 100%;
		height: 100%;
		position: relative;
		z-index: 2;
		.uploader-files {
			width: 100%;
			height: 100%;		
			.bg-img {
				width: 100%;
				height: 100%;
				background-color: #000000;
				.uploader-file {
					width: 100%;
					height: 100%;
					display: flex;
					justify-content: center;
					align-items: center;
					position: relative;
					overflow: hidden;
					image {
						width: 100%;
						height: 100%;
					}
					.cu-tag {
						position: absolute;
						right: 0;
						top: 0;
					}
				}				
			}
			.uploader-input-box {
				width: 100%;
				height: 100%;
				display: flex;
				justify-content: center;
				align-items: center;
				.solids {
					width: 100%;
					height: 100%;
					display: flex;
					justify-content: center;
					align-items: center;
					background-color: rgba(0,0,0,0);
					text[class*="cuIcon-"] {
						color: #FFFFFF;
						font-size: 54rpx;
					}
					&::after {
						border:none
					}
				}
				
			}
		}
	}
</style>
